﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using System.Linq;
using BLL;
using System.Data.Common;
using DAL;
using MainApp.Forms;
using MainApp.Helper;
using System.Threading.Tasks;
using Model;
using System.Xml.Serialization;
using System.IO;

namespace MainApp
{
    static class Program
    {
        static MainWindow mainWindow;
        static FormError formError;
        static Config appConfig;
        static LoadingWindow loadingWindow = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");

            loadingWindow = new LoadingWindow();
            mainWindow = new MainWindow();
            formError = new FormError();
            formError.Owner = mainWindow;

            Initialize();
            //Application.Run(new FormAreaSelector()); return;
            Application.Run(new LoginWindow()); return;
            Application.Run(mainWindow);
        }

        private static void Initialize()
        {
            Task.Factory.StartNew(() =>
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Config));
                try
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + "config.xml";
                    if (File.Exists(path))
                    {
                        using (FileStream stream = new FileStream(path, FileMode.Open))
                        {
                            appConfig = (Config)serializer.Deserialize(stream);
                        }
                    }
                    else
                    {
                        appConfig = new Config { PhotoRootPath = AppDomain.CurrentDomain.BaseDirectory + "Photo\\" };
                        using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
                        {
                            serializer.Serialize(stream, AppConfig);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShowMessageDialog("读取配置文件出错！");
                }

                if (!Directory.Exists(appConfig.PhotoRootPath))
                {
                    Directory.CreateDirectory(appConfig.PhotoRootPath);
                }
                BLL.DataContext.Initialize();
            });
        }

        public static Config AppConfig { get { return appConfig; } }

        public static MainWindow MainForm { get { return mainWindow; } }

        public static void ShowError(Exception ex)
        {
            formError.Show(ex);
        }

        public static void ShowMessageDialog(IEnumerable<string> messages)
        {
            formError.ShowMessage(messages);
        }

        public static void ShowMessageDialog(string text, params string[] messages)
        {
            formError.ShowMessage(text, messages);
        }

        public static void ShowMessage(string text)
        {
            mainWindow.ShowMessage(text);
        }

        public static void ShowValidateInfo(string message)
        {
            MessageBox.Show(message, "数据验证错误！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        internal static void StartProgress()
        {
            mainWindow.StartProgress();
        }

        internal static void StopProgress()
        {
            mainWindow.StopProgress();
        }

        internal static void ShowMessageDialog(string text)
        {
            formError.ShowMessage(text);
        }
    }
}